package com.changhongit.controller.wemeet;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.changhongit.common.Result;
import com.changhongit.mapper.InviteesmeetingMapper;
import com.changhongit.pojo.Inviteesmeeting;
import com.changhongit.service.IInviteesmeetingService;
import com.changhongit.service.wemeet.MeetingService;
import com.changhongit.service.wemeet.WeUserService;
import com.changhongit.vo.InviteesMeetingVo;
import com.tencentcloudapi.wemeet.common.exception.WemeetSdkException;
import com.tencentcloudapi.wemeet.models.meeting.*;
import com.tencentcloudapi.wemeet.models.user.QueryUserDetailResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "腾讯会议会议管理接口")
@RequestMapping("/wemeet/meet")
public class WeMeetController {
    @Autowired
    private MeetingService meetingService;
    @Autowired
    private IInviteesmeetingService iInviteesmeetingService;
    @Autowired
    private WeUserService weUserService;
    @Autowired
    private InviteesmeetingMapper inviteesmeetingMapper;


    //创建会议
    @PostMapping
    @ApiOperation("创建会议")
    public Result createMeeting(@RequestBody CreateMeetingRequest request) throws WemeetSdkException{
        QueryMeetingDetailResponse meeting = meetingService.createMeeting(request);
        return Result.ok(meeting);

    }






    @ApiOperation("设置会议邀请成员")
    @PutMapping("/invitees")
    public Result InvitessMeetingUsers(@RequestBody InviteesMeetingVo inviteesMeetingVo) throws WemeetSdkException {

        System.out.println(inviteesMeetingVo);
        //设置会议邀请成员
        meetingService.inviteesMeetin(inviteesMeetingVo);
        //查询会议邀请成员
        QueryInviteesMeetingUserRequest userRequest = new QueryInviteesMeetingUserRequest();
        userRequest.setMeetingId(inviteesMeetingVo.getMeeting_id());
        userRequest.setInstanceId(inviteesMeetingVo.getInstanceid());
        userRequest.setUserid(inviteesMeetingVo.getUserid());
        QueryInviteesMeetingUserResponse response = meetingService.queryInviteesMeetingUserResponse(userRequest);

        //删除原有数据by meetingid
        QueryWrapper<Inviteesmeeting> wrapper = new QueryWrapper<>();
        wrapper.eq("meetingid",inviteesMeetingVo.getMeeting_id());
        iInviteesmeetingService.remove(wrapper);

        //写入数据库

        for (com.changhongit.pojo.meeting.InviteUser inviteUser:inviteesMeetingVo.getInvites()) {
            Inviteesmeeting inviteesmeeting = new Inviteesmeeting();
            inviteesmeeting.setMeetingid(inviteesMeetingVo.getMeeting_id());
            inviteesmeeting.setInviteuserid(inviteUser.getUserid());
            inviteesmeeting.setSortid(inviteUser.getSortid());
            QueryUserDetailResponse queryUserDetailResponse = weUserService.queryUserDetailResponsebyid(inviteUser.getUserid());
            inviteesmeeting.setNickName(queryUserDetailResponse.getUsername());


            iInviteesmeetingService.save(inviteesmeeting);
        }
        return Result.ok(response);
    }
    @ApiOperation("查看会议邀请成员by顺序")
    @GetMapping("/invitees")
    public Result InviteUsers(Integer pageNum, Integer pageSize, String meetingid) throws WemeetSdkException{
        Page<Inviteesmeeting> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Inviteesmeeting> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(Inviteesmeeting::getMeetingid,meetingid);
        return Result.ok(inviteesmeetingMapper.findInvitees(page,wrapper));
    }










}
